﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using ScriptCompiler2;
using System.Windows.Forms;
//using WindowsFormsApplication1;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Lambda : BaseImageFilter
    {
        protected string m_sLambdaText = null;
        public delegate Color PixelDelegate(Color sourceColor, int x, int y);

        protected CReflectiveEvaluator m_ReflectiveEvaluator = new CReflectiveEvaluator();

        /// <summary>
        /// Initializes a new instance of the <see cref="Lambda"/> class.
        /// </summary>
        public Lambda()
            : this("255 - c.R, 255 - c.G, 255 - c.B")
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Lambda"/> class.
        /// </summary>
        /// <param name="_sLambdaText">The _s lambda text.</param>
        public Lambda(string _sLambdaText)
        {
            string sLowLevelGraphicsPath = Application.StartupPath + @"\LowLevelGraphics.dll";
            m_sLambdaText = _sLambdaText;
            m_ReflectiveEvaluator.AddUsing("System.Windows.Forms.dll");
            m_ReflectiveEvaluator.AddUsing("System.Drawing.dll");
            m_ReflectiveEvaluator.AddUsing(sLowLevelGraphicsPath);
            m_ReflectiveEvaluator.CompilerSuccess += new EventHandler(ev_CompilerSuccess);
            m_ReflectiveEvaluator.CompilerError += new CompilerErrorEventHandler(ev_CompilerError);
            m_ReflectiveEvaluator.TypeCreationError += new TypeCreationErrorEventHandler(ev_TypeCreationError);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Lambda"/> class.
        /// </summary>
        /// <param name="_lambda">The _lambda.</param>
        internal Lambda(Lambda _lambda)
        {
            this.m_sLambdaText = _lambda.m_sLambdaText;
        }

        /// <summary>
        /// Gets or sets the lambda text.
        /// </summary>
        /// <value>The lambda text.</value>
        public string LambdaText
        {
            get { return m_sLambdaText; }
            set { m_sLambdaText = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            string sText = new LambdaScript().Source.Replace("{0}", string.Format("Color.FromArgb({0})", m_sLambdaText));
            object obj = m_ReflectiveEvaluator.Eval(sText, "LambdaScript", "Test", new object[] { _bitmap });
            Debug.WriteLine("Lambdascript: ");
            Debug.WriteLine(sText);
            return _bitmap;
        }

        /// <summary>
        /// Handles the TypeCreationError event of the ev control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ScriptCompiler2.TypeCreationErrorEventArgs"/> instance containing the event data.</param>
        private void ev_TypeCreationError(object sender, TypeCreationErrorEventArgs e)
        {
            Debug.WriteLine(string.Format("Type '{0}' couldn't be created.", e.TypeName));
            Debug.Flush();
        }

        /// <summary>
        /// Handles the CompilerError event of the ev control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ScriptCompiler2.CompilerErrorEventArgs"/> instance containing the event data.</param>
        private void ev_CompilerError(object sender, CompilerErrorEventArgs e)
        {
            Debug.WriteLine(e.Source + Environment.NewLine + " " + e.CompilerErrorCollection[0].ErrorText);
            Debug.Flush();
        }

        /// <summary>
        /// Handles the CompilerSuccess event of the ev control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ev_CompilerSuccess(object sender, EventArgs e)
        {
            Debug.WriteLine("Successfull!");
            Debug.Flush();
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Lambda(this);
        }
    }
}
